#include "FileTransferManager.h"

QMutex FileTransferManager::mMutex;
FileTransferManager* FileTransferManager::mInstance = nullptr;
FileTransferManager::FileTransferManager(QObject* parent)
	:QObject(parent)
{

}

FileTransferManager::~FileTransferManager()
{

}

FileTransferManager* FileTransferManager::getInstance()
{
	QMutexLocker locker(&mMutex);
	if (mInstance == nullptr)
	{
        mInstance = new FileTransferManager();
	}
	return mInstance;
}

FileTransfer* FileTransferManager::addFileTransfer(const QString& name, int port)
{
	FileTransfer* pFileTransfer = mFileTransferMap.value(name);
	if (pFileTransfer == nullptr)
	{
        pFileTransfer = new FileTransfer(this);
		pFileTransfer->start(port);
		mFileTransferMap.insert(name, pFileTransfer);
	}
	return pFileTransfer;
}

void FileTransferManager::removeFileTransfer(const QString& name)
{
	FileTransfer* pFileTransfer = mFileTransferMap.value(name);
	if (pFileTransfer != nullptr)
    {
        mFileTransferMap.remove(name);
        delete pFileTransfer;
    }
}

FileTransfer* FileTransferManager::getFileTransfer(const QString& name) const
{
	return mFileTransferMap.value(name);
}

QList<FileTransfer*> FileTransferManager::getFileTransferList() const
{
	return mFileTransferMap.values();
}

QMap<QString, FileTransfer*>::const_iterator FileTransferManager::getFileTransferMapIter() const
{
	return mFileTransferMap.constBegin();
}

FileTransferManager* getFileTransferManager()
{
	return FileTransferManager::getInstance();
}